﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Fdd.Aspen.Client.ViewModel.Infrastructure;


namespace Fdd.Aspen.Client.Controls
{
    public partial class Error : UserControl
    {
         public static readonly DependencyProperty ShowErrorProperty =
            DependencyProperty.Register(
                            "ShowError",
                            typeof(bool),
                            typeof(Error),
                            new PropertyMetadata(false, ShowErrorPropertyChanged));


        public static readonly DependencyProperty ErrorMessageProperty =
            DependencyProperty.Register(
                                "ErrorMessage",
                                typeof(ViewModelException),
                                typeof(Error),
                                new PropertyMetadata(null, ErrorMessagePropertyChanged));


        public bool ShowError
        {
            get { return (bool)GetValue(ShowErrorProperty); }
            set { SetValue(ShowErrorProperty, value); }
        }


        public Exception ErrorMessage
        {
            get { return (ViewModelException)GetValue(ErrorMessageProperty); }
            set { SetValue(ErrorMessageProperty, value); }
        }


        public Error()
        {
            InitializeComponent();
        }


        private static void ShowErrorPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if ((bool)e.NewValue)
            {
                var makeErrorVisibleStory = ((FrameworkElement)d).Resources["makeErrorVisible"] as Storyboard;

                if (makeErrorVisibleStory != null)
                    makeErrorVisibleStory.Begin();
            }
            else
            {
                var makeErrorHiddenStory = ((FrameworkElement)d).Resources["makeErrorHidden"] as Storyboard;

                if (makeErrorHiddenStory != null)
                    makeErrorHiddenStory.Begin();
            }
        }


        private static void ErrorMessagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var errorMessage = ((FrameworkElement)d).FindName("errorMessage") as TextBlock;

            var exception = e.NewValue as ViewModelException;

            if (exception != null && errorMessage != null)
                errorMessage.Text = exception.Message;
        }


        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            this.ShowError = false;
            this.ErrorMessage = null;
        }


        private void Send_Click(object sender, RoutedEventArgs e)
        {
            this.ShowError = false;
            this.ErrorMessage = null;
        }
    }
}
